package cn.yedong.system.controller;


import cn.yedong.basic.util.AjaxResult;
import cn.yedong.basic.util.QueryResult;
import cn.yedong.system.domain.DictionaryType;
import cn.yedong.system.query.DictionaryTypeQuery;
import cn.yedong.system.service.IDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@ResponseBody
//@Controller
@Api(value="数据字典类型接口类",description = "处理数据字典类型的CRUD操作")
@RestController
@RequestMapping("/dictionaryType")
public class DictionaryTypeController {
    //注入数据字典类型业务的接口
    @Autowired
    private IDictionaryTypeService dictionaryTypeService;

    /**
     * 查询单个对象
     * @param id
     * @return
     */
    //前端： get http://localhost:8080/dictionarytype/1
    @ApiOperation("接口：查询单个对象")
    @GetMapping("/{id}")
    public DictionaryType findOne(@PathVariable("id") Long id) {
        return dictionaryTypeService.loadById(id);
    }

    /**
     * 查询所有
     * @return
     */
    //前端 get http://localhost:8080/dictionarytype
    @ApiOperation("接口：查询所有")
    @GetMapping
    public List<DictionaryType> findAll() {
        return dictionaryTypeService.loadAll();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    //前端： delete http://localhost:8080/dictionarytype/1
    @ApiOperation("接口：删除")
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            dictionaryTypeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }

    /**
     * 添加或修改
     * @param dictionarytype
     * @return
     */
    //前端 put http://localhost:8080/dictionarytype json对象
    @ApiOperation("接口：添加或修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody DictionaryType dictionarytype) {
        try {
            if (dictionarytype.getId() == null) {
                dictionaryTypeService.add(dictionarytype);
            } else {
                dictionaryTypeService.update(dictionarytype);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }

    /**
     * 分页查询
     * @param dictionarytypeQuery
     * @return
     */
    //前端 post http://localhost:8080/dictionarytype json对象
    @ApiOperation("接口：分页查询")
    @PostMapping
    public QueryResult findPage(@RequestBody DictionaryTypeQuery dictionarytypeQuery){
        return dictionaryTypeService.pageQuery(dictionarytypeQuery);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    //前端： delete http://localhost:8080/dictionarytype
    @ApiOperation("接口：批量删除")
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        try {
            dictionaryTypeService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "批量删除失败");
        }
    }

    /**
     * 获取数据字典类型树
     * @return
     */
    //前端 get http://localhost:8080/dictionarytype/deptTree
//    @ApiOperation("接口：查询数据字典类型树")
//    @GetMapping("/deptTree")
//    public List<DictionaryType> deptTree() {
//        return dictionarytypeService.deptTree();
//    }
}
